<?php


namespace app\command;


use app\services\utils\AmapService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class CupboardSetLocation extends Command
{
    protected function configure()
    {
        $this->setName('cupboard_set_location')->setDescription('设置柜机经纬度');
    }

    protected function execute(Input $input, Output $output)
    {
        $list = Db::connect('cupboard')->table('cupboard')
            ->field('id,province,city,district,address')
            ->whereNull('lat')
            ->whereNull('lng')
            ->limit(50)
            ->order('id', 'desc')
            ->select()
            ->toArray();
        if (!$list) {
            return $output->writeln('没有可执行的数据');
        }
        foreach ($list as $value) {
            $address = $value['province'] . $value['city'] . $value['district'] . $value['address'];
            $amap = new AmapService();
            $location = $amap->getCoordinate($address);
            if (!$location) {
                continue;
            }
            Db::connect('cupboard')->table('cupboard')->where('id', $value['id'])->update($location);
        }
        return $output->writeln('执行完毕');
    }
}